package org.apogee.mil.client.portal;

import org.apogee.mil.client.main.MainForm;
import org.apogee.mil.client.portal.pages.AboutForm;
import org.apogee.mil.client.portal.pages.ContactForm;
import org.apogee.mil.client.portal.pages.InformationForm;
import org.apogee.mil.client.widgets.DialogHelper;
import org.apogee.mil.client.widgets.MessageDisplayWidget;
import org.apogee.mil.shared.User;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.custom.Portal;
import com.extjs.gxt.ui.client.widget.custom.Portlet;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Window;

public class PortalForm extends LayoutContainer {
	
	private static final int PORTAL_HEIGHT_OFFSET = 250;
	private static final int PORTLET_HEIGHT_OFFSET = 275;

	private Portlet mainPortlet;
	private MessageDisplayWidget messageDisplay;
	
	@Override  
	protected void onRender(Element parent, int index) {  
		super.onRender(parent, index);  
		setLayout(new FitLayout());  
		
		setSize(Window.getClientWidth(), Window.getClientHeight() - PORTAL_HEIGHT_OFFSET);
		
		Portal portal = new Portal(3);  
		portal.setBorders(true);  
		portal.setColumnWidth(0, .2);  
		portal.setColumnWidth(1, .6);
		portal.setColumnWidth(2, .2);
		portal.setShadow(false);

		final Portlet portlet = new Portlet();    
		portlet.setHeading("Information");  
		configPanel(portlet);  
		portlet.setLayout(new FitLayout());
		
		portlet.addListener(Events.Render, new Listener<BaseEvent>() {

			@Override
			public void handleEvent(BaseEvent be) {
				//portlet.add(new NewsForm());
				//portlet.add(new DownloadForm());
				
				portlet.add(new InformationForm());
			}
		});
		

		portlet.setHeight(Window.getClientHeight() - PORTLET_HEIGHT_OFFSET);  
		portal.add(portlet, 0);

		mainPortlet = new Portlet();  
		mainPortlet.setPinned(true);
		mainPortlet.setHeading("Main");  
		configMenuPanel(mainPortlet);  
		mainPortlet.setLayout(new FitLayout());  
		mainPortlet.add(new MainForm(this));
		mainPortlet.setHeight(Window.getClientHeight() - PORTLET_HEIGHT_OFFSET); 

		portal.add(mainPortlet, 1);  
		
		
		final Portlet debug = new Portlet();  
		debug.setHeading("Messages"); 
		debug.setLayout(new FitLayout());
		debug.setHeight(Window.getClientHeight() - PORTLET_HEIGHT_OFFSET);
		
		
		debug.addListener(Events.Render, new Listener<BaseEvent>() {

			@Override
			public void handleEvent(BaseEvent be) {
				messageDisplay = new MessageDisplayWidget(debug.getWidth(), debug.getHeight());
				debug.add(messageDisplay.asWidget());
				
			}
		});
		
		portal.add(debug, 2);
		configPanel(debug);    

		
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
		centerData.setMargins(new Margins(0));  
		
		add(portal, centerData); 
		
		Window.addResizeHandler(new ResizeHandler() {
			
			@Override
			public void onResize(ResizeEvent event) {
				setSize(event.getWidth(), event.getHeight() - PORTAL_HEIGHT_OFFSET);
				mainPortlet.setHeight(event.getHeight() - PORTLET_HEIGHT_OFFSET);
				debug.setHeight(event.getHeight() - PORTLET_HEIGHT_OFFSET);
				portlet.setHeight(event.getHeight() - PORTLET_HEIGHT_OFFSET);
				
				mainPortlet.layout(true);
				debug.layout(true);
				portlet.layout(true);
				layout(true);
			}
		});
		
	}  

		

	private void configMenuPanel (final ContentPanel panel) {
		panel.setCollapsible(false);
		
		ToolBar tb = new ToolBar();
		tb.setAlignment(HorizontalAlignment.RIGHT);
		
		Button about = new Button("About");
		about.setStyleAttribute("background-color", "#465365");
		about.setStyleAttribute("border-color", "#465365");
		about.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				DialogHelper helper = new DialogHelper("About", new AboutForm().asWidget());
				helper.show();
				
			}
		});
		
		Button contact = new Button("Contact");
		contact.setStyleAttribute("background-color", "#465365");
		contact.setStyleAttribute("border-color", "#465365");
		contact.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				DialogHelper helper = new DialogHelper("Contact", new ContactForm().asWidget());
				helper.show();
			}
		});
		
		tb.add(about);
		tb.add(contact);
		
		panel.getHeader().addTool(tb);
	}

	private void configPanel(final ContentPanel panel) {  
		panel.setCollapsible(true);  
		panel.setAnimCollapse(true);  
		panel.getHeader().addTool(  
				new ToolButton("x-tool-close", new SelectionListener<IconButtonEvent>() {  

					@Override  
					public void componentSelected(IconButtonEvent ce) {  
						panel.removeFromParent();  
					}  

				}));  
	}

	public void getMessages(User currentUser) {
		messageDisplay.update(currentUser);
		
	}

	public void clearMessages() {
		messageDisplay.clearMessages();
		
	}
}